package com.oyc.demo.controller;

import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.oyc.demo.domain.Exporterror;
import com.oyc.demo.service.ExporterrorService;

import io.swagger.annotations.ApiOperation;
/**
 * 导出错误信息列表
 *
 * @author 杜学森
 * @since 2021-01-20 18:51:53
 */
@RestController
@RequestMapping("/exporterror")
public class ExporterrorController {
	
	/**
     * 服务对象
     */
    @Autowired
    private ExporterrorService exporterrorService;
    
    SimpleDateFormat sdf = new SimpleDateFormat("MMddHHmmss");
	
	@RequestMapping(value="/exportexcel",method= RequestMethod.GET)
    @ApiOperation("表格导出")
    public void exportexcel(HttpServletRequest requetst,HttpServletResponse response) {
        try {
        	//省公司合格率
        	List<Exporterror> exporterror=exporterrorService.queryAll(null);
	        
        	 response.setContentType("application/vnd.ms-excel");
             response.setCharacterEncoding("utf-8");
             response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode("成盘电缆检测结果导入失败明细"+sdf.format(new Date()),"UTF-8") + ".xlsx");
             response.setHeader("Set-Cookie", "fileDownload=true; path=/");
             
             ServletOutputStream outputStream = response.getOutputStream();
             try {
             	 EasyExcel.write(outputStream, Exporterror.class).sheet("成盘电缆检测结果导入失败明细").doWrite(exporterror);
             }catch (Exception e){
                 e.printStackTrace();
             }finally {
                 outputStream.close();
             }
            
        }catch (Exception e){
        	e.printStackTrace();
        }
    }
	
	@RequestMapping(value="/selectbycondition",method= RequestMethod.POST)
    @ApiOperation("查询导入错误信息接口")
    public JSONObject selectByCondition(HttpServletRequest requetst) {
			Exporterror exporterror=new Exporterror();
            PageHelper.startPage(Integer.parseInt(requetst.getParameter("page")), Integer.parseInt(requetst.getParameter("rows")));
            //查询数据
            List<Exporterror> listCablebasic = exporterrorService.queryAll(exporterror);
        
        PageInfo<Exporterror> pageInfo = new PageInfo<Exporterror>(listCablebasic);

        JSONObject oJson = new JSONObject();
        oJson.put("records", pageInfo.getTotal());
        oJson.put("total", pageInfo.getPages());
        oJson.put("page", pageInfo.getPageNum());
        
        List<Exporterror> list=pageInfo.getList();
        
        if(list!=null&&list.size()>0){
            JSONArray array=new JSONArray();
            for (int i = 0; i < list.size(); i++) {
            	Exporterror map=list.get(i);
                JSONObject obj=new JSONObject();
                obj.put("id", map.getId());
                
                JSONArray innerarray=new JSONArray();
                innerarray.add(map.getProvincecompanyname());
                innerarray.add(map.getFilename());
                innerarray.add(map.getRownum());
                innerarray.add(map.getReelnumber());
                innerarray.add(map.getErrortype());
                innerarray.add(map.getContent());
                
                obj.put("cell", innerarray);
                array.add(obj);
            }
            oJson.put("rows", array);
        }
        return oJson;
    }
	
}
